<?xml version="1.0" encoding="utf-8" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:lzc="http://schemas.icitadel.com/Lazarus/Compiled.xsd" version="1.0">

  <xsl:output method="text"/>

  <xsl:include href="../../common.xsl" />
  <xsl:include href="../../convert.xsl" />

  <xsl:template match="lzc:project">
    <xsl:apply-templates select="lzc:datasource" />
  </xsl:template>

  <xsl:template match="lzc:datasource">@ECHO OFF
@ECHO . <xsl:value-of select="ancestor::lzc:project/@name" />
@ECHO -----------------------------------------------------
@ECHO Crebas.cmd
@ECHO Creates the SQL Server 2005 Database
@ECHO on the specified instance and schema.
@ECHO .
@ECHO .
@ECHO Syntax: crebas MYPC\SQLEXPRESS MySchema
@ECHO .


:CheckMachine
@ECHO .
@ECHO Checking for specified Machine Name...
IF %1 == "" THEN GOTO :SetDefaultMachine

:SetCustomMachine
@ECHO Machine Name found.  Using '%1'.
@ECHO .
SET DB_MACHINE=%1
GOTO :CheckSchema

:SetDefaultMachine
@ECHO Machine Name not found.  Using '<xsl:value-of select="@location" />'.
@ECHO .
SET DB_MACHINE=<xsl:value-of select="@location" />
GOTO :CheckSchema


:CheckSchema
@ECHO .
@ECHO Checking for specified Schema Name...
IF %2 == "" THEN GOTO :SetDefaultSchema

:SetCustomSchema
@ECHO Machine Name found.  Using '%2'.
@ECHO .
SET DB_SCHEMA=%2
GOTO :CheckMachineDone

:SetDefaultSchema
@ECHO Schema Name not found.  Using '<xsl:value-of select="@schema" />'.
@ECHO .
SET DB_SCHEMA=<xsl:value-of select="@schema" />
:Goto :CheckMachineDone

:CheckMachineDone

:RunScripts
@ECHO .
@ECHO Running Scripts...
@ECHO .
@ECHO *** Step 1: Creating Tables, Columns and Defaults.
sqlcmd -E -i TableCreate.sql -S %1 -d %2
@ECHO .
@ECHO *** Step 2: Adding References (Foreign Keys)
sqlcmd -E -i ReferenceCreate.sql -S %1 -d %2
@ECHO .
@ECHO *** Step 3: Adding Indexes (References and Explicit Indexes)
sqlcmd -E -i IndexCreate.sql -S %1 -d %2
@ECHO .
@ECHO *** Step 4: Adding Constraints
sqlcmd -E -i ConstraintCreate.sql -S %1 -d %2
@ECHO .
@ECHO *** Step 5: Granting Permissions
sqlcmd -E -i Grants.sql -S %1 -d %2
@ECHO .
@ECHO *** Step 6: Adding Initial Data
sqlcmd -E -i InitialData.sql -S %1 -d %2

PUSHD Procedures

@ECHO .
@ECHO *** Step 7: Creating Stored Procedure Stubs
sqlcmd -E -i CreateProcs.sql -S %1 -d %2
@ECHO .
@ECHO *** Step 8: Applying Stored Procedure Implementations
CALL AlterProcs-Integrated.cmd %1 %2

POPD

@ECHO .
@ECHO Database Generation Complete
  </xsl:template>
</xsl:stylesheet>
